package com.report;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import report.com.fengbo.core.util.TimeUtil;

public class ReportRealTime extends DispatchAction {
	private List provice = new ArrayList();
	private List val = new ArrayList();
	public ReportRealTime(){
		addprovice();
	}
	private void addprovice(){
		System.out.println("实例化。。。。。。");
		provice.add("天津");	provice.add("山西");	provice.add("内蒙古");provice.add("黑龙江");
		provice.add("辽宁");	provice.add("吉林");	provice.add("山东");provice.add("河北");
		provice.add("陕西");	provice.add("甘肃");	provice.add("新疆");	provice.add("宁夏");
		provice.add("青海");	provice.add("湖北");	provice.add("湖南");	provice.add("江西");
		provice.add("上海");	provice.add("浙江");	provice.add("江苏");	provice.add("安徽");
		provice.add("河南");	provice.add("福建");	provice.add("广西");	provice.add("四川");
		provice.add("重庆");	provice.add("云南");	provice.add("北京");	provice.add("广东");
		provice.add("江西");
		
		val.add(12);val.add(13);val.add(15);val.add(17);val.add(10);val.add(7);
		val.add(12);val.add(13);val.add(15);val.add(17);val.add(10);val.add(7);
		val.add(12);val.add(13);val.add(15);val.add(17);val.add(10);val.add(7);
		val.add(12);val.add(13);val.add(15);val.add(17);val.add(10);val.add(7);
		val.add(12);val.add(13);val.add(15);val.add(17);val.add(10);
	}
	int p = 0;//省份游标
	int v = 0;//值游标
	/**
	 * 实时返回数据 用于fusioncharts 实时出图
	 * 返回格式 如 &label=0:30:12&value=50|21 每秒返回一次
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward realDate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

			StringBuffer sb = new StringBuffer();
			p = (p>=provice.size())?0:p;
			sb.append("&label="+java.net.URLEncoder.encode(provice.get(p).toString(), "GBK"));
			p++;
			sb.append("&value=5|"+((p%5==0)?25:10)+"|50");
			this.writeBack(response, sb.toString());
			return null;
	}
	/**
	 * amline 实时曲线图
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward realDateAmline(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("动态 amline。。。。。。。");
		 response.setContentType("text/html;charset=utf-8");
		  PrintWriter   out   =   response.getWriter(); 
		  StringBuffer sb = new StringBuffer();
		  
		  StringBuffer vl = new StringBuffer();
		  StringBuffer v2 = new StringBuffer();
		  
		  for(int i = 0;i<provice.size();i++){
			  sb.append("<value xid='"+i+"'>"+provice.get(i)+"</value>");
			  vl.append("<value xid='"+i+"'>"+i%4*p+"</value>");
			  v2.append("<value xid='"+i+"'>"+i%5*p+"</value>");
		  }
		  p++;
		  String data = "<?xml version='1.0' encoding='UTF-8'?><chart>" +
				  
		  		"<series>" +sb.toString()+"</series>" +
		  	
		  		"<graphs>" +
		  		"<graph gid='1'>" +vl.toString()+"</graph>" +
		  		"<graph gid='2'>" +v2.toString()+"</graph>" +
		  		"</graphs></chart>";
		  out.print(data);
		  out.flush();
		  out.close();
		  return null;
	}
	/**
	 * ajax 返回
	 * @param response
	 */
	private void writeBack(HttpServletResponse response,String str){
		try{
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		java.io.PrintWriter out = response.getWriter();
		out.print(str);
		out.flush();
		out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}
